package com.briup.estore.service.impl;

import com.briup.estore.common.pojo.Book;
import com.briup.estore.common.pojo.Collect;
import com.briup.estore.common.utils.BeanCopyUtils;
import com.briup.estore.common.vo.BookInfoVO;
import com.briup.estore.common.vo.BookVO;
import com.briup.estore.dao.BookDao;
import com.briup.estore.dao.CategoryDao;
import com.briup.estore.dao.CollectDao;
import com.briup.estore.dao.impl.BookDaoImpl;
import com.briup.estore.dao.impl.CategoryDaoImpl;
import com.briup.estore.dao.impl.CollectDaoImpl;
import com.briup.estore.service.BookService;

import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午2:21
 * @Description
 **/

public class BookServiceImpl implements BookService {
    private BookDao bookDao=new BookDaoImpl();
    private CategoryDao categoryDao=new CategoryDaoImpl();
    private CollectDao collectDao=new CollectDaoImpl();
    @Override
    public BookInfoVO getBookById(Integer id,int userId) {
        Book book = bookDao.selectById(id);
        String name = categoryDao.findNameById(book.getCategoryId());
        Collect collect = collectDao.findByUserIdAndBookId(userId, id);
        BookInfoVO vo = BeanCopyUtils.copyBean(book, BookInfoVO.class);
        vo.setCategoryName(name);
        vo.setCollect(collect==null?false:true);
        return vo;
    }

    @Override
    public List<BookVO> getBook(String query) {
        List<Book> books = bookDao.findByQuery(query);
        List<BookVO> list = BeanCopyUtils.copyList(books, BookVO.class);
        return list;
    }

    @Override
    public List<BookVO> getBooksByCategoryId(Integer categoryId) {
        List<Book> books = bookDao.selectByCategoryId(categoryId);
        List<BookVO> vos = BeanCopyUtils.copyList(books, BookVO.class);
        return vos;
    }

    @Override
    public List<BookVO> showBooks() {
        List<Book> books = bookDao.selectBooksBefore20();
        List<BookVO> vos = BeanCopyUtils.copyList(books, BookVO.class);
        return vos;
    }


}
